﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coliseu.BussinessLayer;

namespace coliseu.Views.Rotinas_Diarias
{
    public partial class FrmCadVisitante : Form
    {
        private VisitanteBL vBL;
        private visitante v;
        private string tipo_imagem;

        public FrmCadVisitante(VisitanteBL vBL)
        {
            InitializeComponent();
            this.vBL = vBL;
            visitanteBindingSource.AddNew();

            fotoPictureBox.Image = Properties.Resources.clear;
            tipo_imagem = "png";
        }

        public FrmCadVisitante(VisitanteBL vBL, visitante v)
        {
            InitializeComponent();
            this.vBL = vBL;
            this.v = v;

            id_visitanteTextBox.Enabled = false;
            visitanteBindingSource.DataSource = v;

            fotoPictureBox.Image = Classes.Utils.byteArrayToImage(((visitante)visitanteBindingSource.Current).foto);
        }

        private void FrmCadVisitante_Load(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ValidateChildren();
            Validate();
            if (!string.IsNullOrEmpty(tipo_imagem))
                ((visitante)visitanteBindingSource.Current).foto = Classes.Utils.imageToByteArray(fotoPictureBox.Image, tipo_imagem);

            visitanteBindingSource.EndEdit();

            if (v == null)
                vBL.salvar((visitante)visitanteBindingSource.Current, "insert");
            else
                vBL.salvar((visitante)visitanteBindingSource.Current, "edit");

            MessageBox.Show("Salvo com sucesso", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
            openFileDialog1.ShowDialog();
            if (!string.IsNullOrEmpty(openFileDialog1.FileName))
            {
                fotoPictureBox.ImageLocation = openFileDialog1.FileName;
                tipo_imagem = openFileDialog1.SafeFileName.Split('.')[1];
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            fotoPictureBox.Image = Properties.Resources.clear;
            ((visitante)visitanteBindingSource.Current).foto = Classes.Utils.imageToByteArray(Properties.Resources.clear, "png");
        }
    }
}
